<template>
  <div id="container">
    <el-row>
      <el-col :span="12">
        <el-form ref="form" :model="expense" label-width="80px">
          <el-form-item label="股票代码">
            <el-input v-model="myStock.stockCode" style="width: 160px"></el-input>
          </el-form-item>
          <el-form-item label="股票名称">
            <el-input v-model="myStock.stockName" style="width: 160px"></el-input>
          </el-form-item>
          <el-form-item label="买入价格">
            <el-input v-model="myStock.stockPrice" style="width: 80px"></el-input>
          </el-form-item>
          <el-form-item label="买入手数">
            <el-input v-model="myStock.stockNum" style="width: 80px"></el-input>
          </el-form-item>
          <el-form-item label="买入时间">
            <el-col :span="11">
              <el-date-picker
                v-model="myStock.stockTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="用户编号">
            <el-input v-model="myStock.stockUserId" style="width: 80px"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="myStock.stockUser" style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即添加</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>

<script>
export default {
  name: "add",
  data() {
    return {
      myStock: {
        //stockId: 1,
        stockCode: '',
        stockName: '',
        stockPrice: 0,
        stockNum: 0,
        //marketValue: 27740,
        stockTime: '',
        //currentPrice: 12.19,
        //currentMarketValue: 24380,
        //profitLoss: 0,
        stockUser: this.$store.state.user.realName,
        stockUserId: this.$store.state.user.uid
      }
    }
  },
  mounted() {
  },
  methods: {
    onSubmit() {
      //判断一下属于什么板块。
      if(this.myStock.stockCode.startsWith('0')){
        this.myStock.stockType = '深圳板块';
      }else if(this.myStock.stockCode.startsWith('300')){
        this.myStock.stockType = '创业板块'
      }
      else{
        this.myStock.stockType = '上海板块'
      }

      this.$store.dispatch('addUserStock', this.myStock).then(resp => {
        if (resp.code === 200) {
          this.$message({
            type: 'success',
            message: resp.msg
          })

          this.myStock.stockCode = '';
          this.myStock.stockName = '';
          this.myStock.stockPrice = 0.0;
          this.myStock.stockNum = 0;
          this.myStock.stockTime = '';



        }
      })
    }
  }
}
</script>

<style scoped>
#container {
  margin: 20px 10px;
  padding-right: 20px;
}

.demo-form-inline {
  text-align: center;
}
</style>
